<?php
/**
 * Backup handling for resourcepage. Unlike some other backup files, 
 * the module's data structures are not documented here. Please see
 * the proper documentation (on wiki) for a database diagram and
 * detailed description.
 *
 * @copyright &copy; 2006 The Open University
 * @author s.marshall@open.ac.uk
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
 * @package package_name
 *//** */
  
function refworks_backup_mods($bf,$preferences) {
    
    global $CFG;

    $status = true;

    //Iterate over resourcepage table
    $mods = get_recordset ('refworks','course',$preferences->backup_course,"id");
    if ($mods->RecordCount()) {
        while ($mod = rs_fetch_next_record($mods)) {
            if (backup_mod_selected($preferences,'refworks',$mod->id)) {
                $status &= refworks_backup_one_mod($bf,$preferences,$mod);
            }
        }
        rs_close($mods);    return $status;
    }
    
    return $status;
}

function refworks_backup_one_mod($bf,$preferences,$mod) {

    global $CFG;

    if (is_numeric($mod)) {
        $mod = get_record('refworks','id',$mod);
    }

    //Start mod
    $status=fwrite ($bf,start_tag("MOD",3,true));
    fwrite ($bf,full_tag("ID",4,false,$mod->id));
    fwrite ($bf,full_tag("MODTYPE",4,false,"refworks"));
    fwrite ($bf,full_tag("NAME",4,false,$mod->name));
    fwrite ($bf,full_tag("TIMECREATED",4,false,$mod->timecreated));
    fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$mod->timemodified));
    fwrite ($bf,full_tag("AUTOPOPFOLDER",4,false,$mod->autopopfolder));
    fwrite ($bf,full_tag("AUTOPOPDATA",4,false,$mod->autopopdata));  
    //End mod
    $status &= fwrite ($bf,end_tag("MOD",3,true));

    return $status;
}

////Return an array of info (name,value)
function refworks_check_backup_mods($course,$user_data=false,$backup_unique_code,$instances=null) {
    
    if (!empty($instances) && is_array($instances) && count($instances)) {
        $info = array();
        foreach ($instances as $id => $instance) {
            $info += refworks_check_backup_mods_instances($instance,$backup_unique_code);
        }
        return $info;
    }
    //First the course data
    $info[0][0] = get_string('modulenameplural','refworks');
    $info[0][1] = count_records('refworks','course',$course);
    
    return $info;
}

////Return an array of info (name,value)
function refworks_check_backup_mods_instances($instance,$backup_unique_code) {
    //Basic data
    $info[$instance->id.'0'][0] = '<b>'.$instance->name.'</b>';
    $info[$instance->id.'0'][1] = '';
    
    return $info;
}


//Return a content encoded to support interactivities linking. Every module
//should have its own. They are called automatically from the backup procedure.
function refworks_encode_content_links ($content,$preferences) {

    global $CFG;

    $base = preg_quote($CFG->wwwroot,"/");

    //Link to the list of resourcepages
    $buscar="/(".$base."\/mod\/refworks\/index.php\?id\=)([0-9]+)/";
    $result= preg_replace($buscar,'$@REFWORKSINDEX*$2@$',$content);

    //Link to resourcepage view by moduleid
    $buscar="/(".$base."\/mod\/refworks\/view.php\?id\=)([0-9]+)/";
    $result= preg_replace($buscar,'$@REFWORKSVIEWBYID*$2@$',$result);

    return $result;
}

